home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Suzy B Software 2
/
Suzy B Software CD-ROM 2 (1994).iso
/
picmanip
/
pic_r2z
/
rshade30
/
viewmtv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-05
|
2KB
|
92 lines
#include <stdio.h>
#include <stdlib.h>
typedef enum { FALSE, TRUE } boolean;
typedef unsigned char BYTE;
/* Interface to John Logajan's gray-level display routines. */
extern void grayinit(void), grayexit(void), grayshow(void);
extern void gray22(BYTE *address, long span);
boolean read_file(FILE *fp, BYTE *mem, int w, int h)
{
BYTE *rbuf, *rover;
int x, y;
long conv;
if ((rbuf = calloc((size_t)w, (size_t)3)) == NULL)
return FALSE;
for (y = 0; y < h; ++y) {
if (fread(rbuf, (size_t)3, (size_t)w, fp) < (size_t)w) {
free(rbuf);
return FALSE;
}
for (x = 0, rover = rbuf; x < w; ++x) {
conv = *rover++ * 299l /* calc luminance (NTSC Y formula) */
+ *rover++ * 587l
+ *rover++ * 114l;
*mem++ = (BYTE)(conv / 11591); /* scale to range 0..21 */
}
}
free(rbuf);
return TRUE;
}
void main(int argc, char **argv)
{
FILE *file;
int width, height;
BYTE *buffer;
if (argc != 2) {
printf("Usage: viewmtv filename\n");
exit(1);
}
if ((file = fopen(argv[1], "rb")) == NULL) {
printf("Can't open '%s'\n", argv[1]);
exit(1);
}
if (fscanf(file, "%d %d", &width, &height) != 2) {
fclose(file);
printf("'%s' is not in MTV format\n");
exit(1);
}
while (getc(file) != '\n') /* skip MTV `header' line */
;
if ((buffer = malloc((long)width * (long)height)) == NULL) {
fclose(file);
printf("Not enough memory for %dx%d pixels\n", width, height);
exit(1);
}
if (! read_file(file, buffer, width, height)) {
fclose(file);
printf("Error while reading '%s'\n", argv[1]);
exit(1);
}
fclose(file);
/* ### */
grayinit();
gray22(buffer, width);
grayexit();
/* ### */
free(buffer);
exit(0);
}